

import UIKit

public class TF_SignIn: UIView {
    @objc public weak var delegate : TF_SignInDelegate?
    
    @objc public let view = UIView()
    //总时长
    @objc public var duration:NSInteger = 0

    //签到的进度圈
    @objc public let round = TF_LivingProgress()
    
    @objc public var result:TFSignNew?
    
    @objc public let signIn = UILabel()
    @objc public let timeLabel = UILabel()
    @objc public var timer:Timer?
    @objc public let avatarView = UIImageView()
    @objc public let successView = UIImageView()
    @objc public let title = UILabel()
    @objc public let prompt = UILabel()
    @objc public let successLabel = UILabel()

    @objc public let clostBtn = UIButton()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.view.layer.shadowRadius = 12
        self.view.layer.shadowOpacity = 1
        self.view.layer.cornerRadius = 10
        self.view.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
        self.addSubview(self.view)
        
        self.view.addSubview(self.round)
        self.round.backgroundColor = UIColor(red: 235/255.0, green: 245/255.0, blue: 255/255.0, alpha: 1.0)
        self.round.layer.masksToBounds  =  true
       
        //签到
        self.signIn.numberOfLines = 0;
        self.round.addSubview(self.signIn)
        
        let signInColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
        self.upaDataLanguage()
        
        self.signIn.textColor = signInColor
        self.signIn.textAlignment = .center;
        

        self.timeLabel.numberOfLines = 0;
        self.addSubview(self.timeLabel)
        self.timeLabel.font = UIFont.init(name: "San Francisco Display", size: 13)
        self.timeLabel.text = "00:00";
        self.timeLabel.textAlignment =  .center
        self.timeLabel.textColor = UIColor(red: 136/255.0, green: 196/255.0, blue: 255/255.0, alpha: 1.0)
        self.round.addSubview(self.timeLabel)
        
  
        self.view.addSubview(self.avatarView)
        self.avatarView.clipsToBounds = true
        

        self.title.numberOfLines = 0
        self.title.textColor = UIColor(red: 157/255.0 , green: 162/255.0, blue: 167/255.0, alpha: 1.0)
        
        self.title.alpha = 1.0
        self.view.addSubview(self.title)
        self.title.font = UIFont.PingFang(size: 14)

        
        self.prompt.numberOfLines = 0;
        self.prompt.textColor = UIColor(red: 157/255.0 , green: 162/255.0, blue: 167/255.0, alpha: 1.0)
        self.prompt.alpha = 1.0
        self.view.addSubview(self.prompt)
        self.prompt.textAlignment = .center;
        
        
        let  promptStr:NSString = "请点击 签到按钮 完成点名"
        let fontAttributeNameStr = NSMutableAttributedString.init(string: promptStr as String)

        let font18 = UIFont.PingFang(size: 18)
        let attr18: [NSAttributedString.Key : Any] = [.font: font18 as Any,]
        
        fontAttributeNameStr.addAttributes(attr18, range: NSRange(location:0, length: 3 ))
        fontAttributeNameStr.addAttribute(.foregroundColor, value: UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0), range: NSRange(location: 0, length: 3))
        
        fontAttributeNameStr.addAttribute(.foregroundColor, value: UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0), range: NSRange(location: 4, length: 4))

        fontAttributeNameStr.addAttribute(.foregroundColor, value: UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0), range: NSRange(location: 8, length: 5))

        
        self.prompt.attributedText = fontAttributeNameStr;
        

        self.view.addSubview(self.successView)
        self.successView.image = UIImage.init(named: "HTLive.bundle/liveingtoupiaosuccess")
        self.successView.isHidden = true
        
        
        self.successLabel.isHidden = true
        self.successLabel.numberOfLines = 0
        self.view.addSubview(self.successLabel)

        
        self.successLabel.attributedText = NSMutableAttributedString.getAttributed(string:"投票成功" , font: UIFont.PingFang(size: 18), foregroundColor: signInColor);
        self.successLabel.textColor = signInColor
        self.successLabel.textAlignment = .center
        
        
        self.clostBtn.setImage(UIImage.init(named: "HTLive.bundle/close_gray"), for: .normal)
        self.clostBtn.addTarget(self, action: #selector(self.clostBtnClick), for: .touchUpInside)
        self.view.addSubview(self.clostBtn)
    }
    
    @objc func clostBtnClick()
    {
        self.viewHidde()
    }
    @objc public func roundClickUI()
    {
        self.prompt.isHidden = true
        self.round.isHidden = true
        self.avatarView.isHidden = true
        self.title.isHidden = true
        
        self.successView.isHidden = false
        self.successLabel.isHidden = false
        self.successLabel.text = "签到成功"
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        self.round.frame = CGRect(x: (self.frame.size.width - 120) / 2, y: self.view.frame.size.height - 120 - 58, width: 120, height: 120)
        
        self.round.layer.cornerRadius = self.round.bounds.size.width / 2
        
        self.timeLabel.frame = CGRect(x: 0, y: self.round.frame.size.height - 32 - 16, width: self.round.frame.size.width, height: 16)

        
        self.signIn.frame = CGRect(x: 0, y: 36, width: self.round.frame.size.width, height: 30)
      
        
        self.avatarView.frame = CGRect(x: 20, y: 20, width: 22, height: 22)
     
        
        self.avatarView.layer.cornerRadius = self.avatarView.bounds.size.width / 2
        
        self.title.frame = CGRect(x: 50, y: 21, width: self.frame.size.width - 50, height: 20)
        

        
        self.prompt.frame = CGRect(x: 0, y: 80, width: self.view.frame.size.width, height: 25)
   
        
        
        self.successView.frame = CGRect(x: (self.view.frame.size.width - 200) / 2, y: (self.view.frame.size.height - 137 ) / 2, width: 200, height: 137)

        
        self.successLabel.frame = CGRect(x: 0, y: self.view.frame.size.height - 25 - 64, width: self.view.frame.size.width, height: 25)

        self.clostBtn.frame = CGRect(x: self.frame.size.width-40, y: 10, width: 30, height: 30)
        
    }
    ///发起签到
    @objc public func onSignNewUI(result:TFSignNew)
    {

        self.prompt.isHidden = false
        self.round.isHidden = false
        self.avatarView.isHidden = false
        self.title.isHidden = false
        

        self.successView.isHidden = true
        self.successLabel.isHidden = true
        
        //主播
        self.title.text = "主播发起了点名"

        self.avatarView.sd_setImage(with: URL.init(string: result.avatar))
        
        self.result = result;
        self.destroySharedIntance()


        self.duration = result.duration;
        
        self.view.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 312)
        
        UIView.animate(withDuration: 1) {
            self.view.frame = CGRect(x: 0, y: self.frame.size.height - 312, width: self.frame.size.width, height: 312)
    
        }

        self.countdown()
     
    }

    //倒计时
    @objc func countdown()
    {
        let text:String = (TalkfunSharedTools.getCountdownWithTime( self.duration) as NSString) as String
        self.timeLabel.text = text
       
        if((self.timer) == nil)
        {
            self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(countdown), userInfo: nil, repeats: true)
            RunLoop.main.add(self.timer! , forMode: RunLoop.Mode.default)
            self.timer!.fire()
        }else{
     
            self.duration = self.duration - 1
            
            if (self.duration == 0) {
                self.timeLabel.text = "0"
                
          
                self.destroySharedIntance()
                self.viewHidde()
                
                if self.delegate != nil && (self.delegate?.responds(to: #selector(TF_SignInDelegate.signEnd) )   )!{
                    
                    self.delegate?.signEnd()
                    
                }
                
            }
            
            
        }
        
        let duration1:CGFloat = CGFloat(self.duration)
        let duration12:CGFloat = CGFloat(self.result!.duration)
        let progress:CGFloat =  duration1 / duration12;
        //倒计进度
        self.round.progressUI(value: progress)
   
    }
    /**
     @name 接收主播发起结束签到的事件回调
    */
    @objc public func onSignEndUI(result:TFSignEnd)
    {
        self.destroySharedIntance()

        self.duration = 0
        
        self.viewHidde()
      
    }
    @objc func upaDataLanguage()
    {

        let signInColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
        self.signIn.attributedText = NSMutableAttributedString.getAttributed(string:"签到" , font: UIFont.PingFang(size: 22), foregroundColor: signInColor)
    }

    func viewHidde()
    {
        if (self.view.frame.origin.y == self.frame.size.height - 312 ) {
            
            UIView.animate(withDuration: 1) {
                
                self.view.frame = CGRect(x: 0, y:UIScreen.main.bounds.height, width: self.frame.size.width, height: 312)
           
            }
        }
    }

    @objc public func destroySharedIntance()
     {
         if self.timer != nil {
             self.timer?.invalidate()
             self.timer = nil
         }
     }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01 ) {
            return nil
        }

        
        // 2.判断下点在不在当前控件上
        // 用pointInside方法判断点在不在控件上,point这个必须是方法调用者坐标系上的点
        if (self.point(inside: point, with: event) == false){
            return nil
        }
        // 3.从后往前遍历自己的子控件
        
        let count = self.subviews.count
        
        //倒序
        for i in (0..<count).reversed() {

        // 取出子控件
        let childV = self.subviews[i] as UIView
            
        // 把自己坐标系上点转化成子控件坐标系上点
          let  childP =  self.convert(point, to: childV)
          let fitView =  childV.hitTest(childP, with: event)
             
            // 如果找到最合适view就直接返回
            if ((fitView) != nil) {
                return fitView;
            }
            
        }
     
        self.viewHidde()
        return nil

    }

}
@objc public  protocol TF_SignInDelegate : NSObjectProtocol {
    
    func signEnd()
  
}
